using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LiftingParts : MonoBehaviour
{
    public GameObject liftParts;
    public List<GameObject> liftPartList;
    public List<Vector3> targetHeightPerLiftPartList;
    public AGVObject agvObject;
    //public float height;
    public float target;

    public bool bIsMovingDown = false;

    private Vector3 unitTargetHeight;
    public float unit_height = 2;
    // Start is called before the first frame update
    void Start()
    {
        unitTargetHeight = new Vector3(0, unit_height, 0);
        for (int i = 0; i < liftPartList.Count; i++)
        {
            targetHeightPerLiftPartList.Add(unitTargetHeight);
        }
    }

    public void LiftUp(float height)
    {
        int movingLiftPartIndex = 0;
        if (height < 0)
        {
            return;
        }
        for (int i = 0; i < liftPartList.Count; i++)
        {
            height = target - liftPartList[i].transform.localPosition.y;
            if (height >= (i + 1) * 2 && height < (i + 2) * 2)
            {
                movingLiftPartIndex = i;
            }
            else if (liftPartList[movingLiftPartIndex].transform.localPosition.y == targetHeightPerLiftPartList[movingLiftPartIndex].y && movingLiftPartIndex < liftPartList.Count - 1)
            {
                movingLiftPartIndex++;
            }
        }
        liftPartList[movingLiftPartIndex].transform.localPosition = Vector3.MoveTowards(liftPartList[movingLiftPartIndex].transform.localPosition, targetHeightPerLiftPartList[movingLiftPartIndex], Time.deltaTime * agvObject.LiftingSpeed);
    }

    float tmp_height = 0;
    public void LiftDown(float h = 0)
    {
        int index = 0;
        h -= 1.5f;
        float delta_height = 0;
        for (int i = 0; i < liftPartList.Count; i++)
        {
            delta_height += liftPartList[i].transform.localPosition.y;
        }
        if (delta_height <= h)
        {
            bIsMovingDown = false;
            return;
        }
        for (int i = 0; i < liftPartList.Count; i++)
        {
            //Debug.Log("liftPartList[" + i + "].transform.localPosition = " + liftPartList[i].transform.localPosition);
            if (liftPartList[index].transform.localPosition.y != 0)
            {
                liftPartList[index].transform.localPosition = Vector3.MoveTowards(liftPartList[index].transform.localPosition, Vector3.zero, Time.deltaTime * agvObject.LiftingSpeed);
                tmp_height +=
                    Vector3.Distance(liftPartList[index].transform.localPosition, Vector3.zero) > Time.deltaTime * agvObject.LiftingSpeed
                    ? Time.deltaTime * agvObject.LiftingSpeed
                    : Vector3.Distance(liftPartList[index].transform.localPosition, Vector3.zero);
                break;
            }
            else if (liftPartList[index].transform.localPosition.y == 0)
            {
                index++;
            }
        }
        if (index == liftPartList.Count)
        {
            bIsMovingDown = false;
        }

    }
    public void LiftToTarget(float height)
    {
        height -= 1.5f;
        for (int i = 0; i < liftPartList.Count; i++)
        {
            height = target - liftPartList[i].transform.localPosition.y;
            if (height >= 2 && height < 4)
            {
                liftPartList[i].transform.localPosition = new Vector3(liftPartList[i].transform.localPosition.x, height - targetHeightPerLiftPartList[i].y, liftPartList[i].transform.localPosition.z);
            }
            else if (height >= 4)
            {
                liftPartList[i].transform.localPosition = new Vector3(liftPartList[i].transform.localPosition.x, targetHeightPerLiftPartList[i].y, liftPartList[i].transform.localPosition.z);
            }
        }
    }

}
